"""
Convert static species data to rows in a separate table

"""

#################### Set up Django environment
import sys,os
from datetime import *

os.environ['DJANGO_SETTINGS_MODULE'] ='treedata.settings'

from django.core.management import setup_environ
from treedata import settings
setup_environ(settings)

#################### Import models from Django project

from trees.models import *
from django.contrib.auth.models import User, Group
#################### Start work

plants = Plant.objects.filter(id__gt=30538)

for p in plants:
    text = p.species_o
    try:
        text = text.replace("\"","")
        plantarr = text.split("::")
        variety = plantarr[0].strip()
        common = plantarr[1].strip()
    except:
        pass
        
    # Check whether current plant has a species field
    # Check whether species is in the table
    # If so, get that Species object
    # If not, create it and get the new object
    # Set the current Plant to the Species
    if variety:
        try:
            specie = Species.objects.get(species__iexact=variety)
        except:
            specie = Species()
            specie.species = variety
            specie.common_name = common
            specie.save()
        
        p.species = specie
        p.save()
            
            
    # print species
    print p.id
    print variety
    print common
    
    print "------"
    print



  
        
    
        
 
        
                      
